<template>
  <div></div>
</template>

<script setup lang="ts">
// import { onMounted, nextTick } from 'vue'
import { ElLoading } from 'element-plus'
import { useRouter } from 'vue-router'
import AuthService from '@/api/service/AuthService'
import useGlobalStore from '@/store/modules/global'

// 全局缓存
const globalStore = useGlobalStore()

const router = useRouter()

const loading = ElLoading.service({
  lock: true,
  text: '正在验证授权信息',
  background: 'rgba(0, 0, 0, 0.4)'
})
// 检查localStorage-token
const checkLocalStorageToken = () => {
  let res = false
  const token = localStorage.getItem('tokenMediaPlat') || '' || globalStore.token
  if (token) {
    globalStore.setToken(token)
    res = true
  }
  return res
}
// autoLogin 已经授权获取用户信息
const handleAutoLogin = async () => {
  const checkVal = checkLocalStorageToken()
  if (!checkVal) {
    if (import.meta.env.VITE_USER_NODE_ENV === 'development') {
      router.push('/login')
    } else {
      window.open(`${import.meta.env.VITE_BASE_URL}${import.meta.env.VITE_UNIVERSE_LOGIN}`, '_self')
    }
    loading.close()
    return
  }
  const res = await AuthService.getUserInfo()
  if (res && res.code === globalStore.res_success) {
    globalStore.setUserInfo(res.result.detail)
    router.push('/create')
  }
  // 从元宇宙获取permissions
  const sourceData = JSON.parse(localStorage.getItem('userMediaPlatInfo') as any)
  globalStore.setPermissions(sourceData?.permissions || [])
  loading.close()
}
const handleRouterInfo = async () => {
  if (router.currentRoute.value.query.autoLogin === '1') {
    await handleAutoLogin()
  }
}
handleRouterInfo()
</script>

<style scoped lang="scss"></style>
